package android.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import winkyle.woodenfish.android.exception.FileStoreException;
import winkyle.woodenfish.android.exception.LoginException;
import winkyle.woodenfish.android.service.UserInfoService;
import winkyle.woodenfish.constant.CookieConstant;
import winkyle.woodenfish.enums.ResultEnum;
import winkyle.woodenfish.model.dto.UserDto;
import winkyle.woodenfish.model.form.UserLoginForm;
import winkyle.woodenfish.model.vo.UserVo;
import winkyle.woodenfish.util.BindingResultUtil;
import winkyle.woodenfish.util.CookieUtil;
import winkyle.woodenfish.util.PathUtil;
import winkyle.woodenfish.util.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@RestController
@RequestMapping(value = "/user")
public class UserInfoApi {
	private final Logger logger= LoggerFactory.getLogger(UserInfoApi.class);
	@Autowired
	private UserInfoService userInfoService;
	
	@PostMapping(value = "/login")
    public Object login(@Valid UserLoginForm userLoginForm,BindingResult bindingResult,HttpServletResponse httpServletResponse) {	
		BindingResultUtil.verify(bindingResult);
		logger.info("用户账号是:"+userLoginForm.getAccount()+",密码是:"+userLoginForm.getPassword());
		UserDto userDto = userInfoService.verifyAccount(userLoginForm.getAccount(), userLoginForm.getPassword());
		if(userDto != null){
			//TODO 将某个值存入Cookie,将设备号，Id，以及登录来源保留进redis，以便每次接口访问前（通过 {@link PermissionAspect}）往redis查找信息判断登录者是否合法登录
			CookieUtil.set(httpServletResponse, CookieConstant.LONGIN_KEY, userDto.getUserId(), CookieConstant.COOKIEMAXAGE);
			return ResultUtil.success();
		}else {
			throw new LoginException(ResultEnum.LOGINFAILED);
		}
    }
	
	@GetMapping(value = "/userInfoByUserId")
    public Object getUserInfoByUserId(HttpServletRequest httpServletRequest) {	
		logger.info("获取{}的用户信息",CookieUtil.get(httpServletRequest, CookieConstant.LONGIN_KEY).getValue());
		UserDto userDto = userInfoService.findUserInfoByUserId(CookieUtil.get(httpServletRequest, CookieConstant.LONGIN_KEY).getValue());
		UserVo userVo = new UserVo();
		userVo.setNickName(userDto.getNick());
		userVo.setAccount(userDto.getAccount());
//		userVo.setBirthday(userDto.getBirthday());
//		userVo.setCompany(userDto.getCompany());
		if (userDto.getHeadPortrait()!=null) {
			userVo.setHeadPortrait(PathUtil.getAccessPath(httpServletRequest)+userDto.getHeadPortrait());
		}else {
			userVo.setHeadPortrait(null);
		}
//		userVo.setNick(userDto.getNick());
//		userVo.setPost(userDto.getPost());
//		userVo.setSex(userDto.getSex());
//		userVo.setUserId(userInfo.getUserId());
		return ResultUtil.success(userVo);
    }
	
	
	@PutMapping(value = "/headPortraitByUserId")
    public Object changeHeadPortraitByUserId(MultipartFile headPortrait,HttpServletRequest httpServletRequest){	
		String path = 
		userInfoService.modifyHeadPortraitByUserId(CookieUtil.get(httpServletRequest, CookieConstant.LONGIN_KEY).getValue(),
				PathUtil.getAbsolutePath(httpServletRequest), headPortrait);
		if(path!=null) {
			return ResultUtil.success(PathUtil.getAccessPath(httpServletRequest)+path);
		}
		else {
			throw new FileStoreException("文件为空");
		}
    }
	
	
}
